package com.jlkf.controller.website;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jlkf.common.Coder;
import com.jlkf.service.website.LoginService;
import com.platform.server.response.response.pojo.ReturnData;

/**
 * 网站相关接口 将加入到 website包下
 * @author DN
 *
 */
@Controller
@RequestMapping("website")
public class LoginController {

	@Autowired
	private LoginService loginService;
	/**
	 * 乘客、司机登录 
	 * @return
	 */
	@RequestMapping("getPassengersLogin")
	public Object getPassengersLogin(@RequestBody String json){
		return loginService.getPassengersLogin(json);
	}
	
	//我是乘客  忘记密码
	@RequestMapping("forgetPassword")
	public Object forgetPassword(@RequestBody String json){
		return loginService.forgetPassword(json);
	}
	
	//网站注册 司机注册与企业注册
	@RequestMapping("registration")
	public Object registration(@RequestBody String json){
		return loginService.registration(json);
	}
	//网站 司机注册
	@RequestMapping("driverRegistration")
	public Object driverRegistration(@RequestBody String json){
		return loginService.driverRegistration(json);
	}
	
	/**
	 * 企业登录 企业使用账号id与密码登录
	 * @param json
	 * @return
	 */
	@RequestMapping("getEnterpriseLogin")
	public Object getEnterpriseLogin(@RequestBody String json){
		return loginService.getEnterpriseLogin(json);
	}
	//更新我的个人信息
	@RequestMapping("updateUserInfo")
	public Object updateUserInfo(@RequestBody String json){
		return loginService.updateUserInfo(json);
	}
	//Purchases via Direct Post   商户向用户划款
	@RequestMapping("payCallBack")
	public void payCallBack(Integer r,Boolean successful,String message,Integer amount,String id,String token,String card_holder,String card_number,
			String card_expiry,String reference,String v,List<String> errors){
		System.out.println("r="+r);System.out.println("successful="+successful);
		System.out.println("message="+message);
		System.out.println("amount="+amount);
		System.out.println("id="+id);
		System.out.println("token="+token);
		System.out.println("card_holder="+card_holder);
		System.out.println("card_number="+card_number);
		System.out.println("card_expiry="+card_expiry);	
		System.out.println("errors="+errors);
		System.out.println("reference="+reference);
		System.out.println("v="+v);
	}
	
	@RequestMapping("getPay")
	public Object getPay() throws Exception{
		 URL url = new URL("https://gateway-sandbox.pmnts.io/v2/purchases/direct/TESTFAB");
	      HttpsURLConnection conn = null;
	      PrintWriter out = null;
	      BufferedReader in = null;
	      String result = "";
	      conn = (HttpsURLConnection)url.openConnection();
	      SSLContext sc = SSLContext.getInstance("TLSv1.2");
	      sc.init(null, null, new java.security.SecureRandom());
	      conn.setSSLSocketFactory(sc.getSocketFactory());
	      conn.setRequestProperty("accept", "*/*");
	     conn.setRequestProperty("Content-type", "application/json");
	      conn.setRequestProperty("Authorization","Basic VEVTVEZBQjo5NjdjNDI1M2I2ZjllZDc5MGYzNjljODJjOGRjMzBiYTIxZjA3ZWJi");
	      conn.setDoOutput(true);
	      conn.setDoInput(true);
	      conn.setUseCaches(false);
//	      out.println("{\"card_holder\":\"test\",\"card_token\":\"n30gnpy6\",\"cvv\":\"123\",\"amount\":10,\"reference\":\"nidsds\",\"customer_ip\":\"101.101.101.101\",\"currency\":\"AUD\"}");
	      out = new PrintWriter(conn.getOutputStream());
//	      out.print("{\"wallet\":{\"type\":\"VISA\",\"callid\":\"4005550000000001\"}}");
	      String inputStr = "FAB201712222214:20:AUD:https://47.52.72.90:8080/fab_api/website/payCallBack";
	      byte[] inputData = inputStr.getBytes();
	      String key = "c7178bb0d8";
	     //hmaxmd5
	      String verification = Coder.byteArrayToHexString(Coder.encryptHMAC(inputData, key));
	      System.out.println(verification);
	     out.print("{\"card_number\":\"345678901234564\",\"card_holder\":\"test\",\"expiry_month\":\"12\",\"expiry_year\":\"2023\",\"cvv\":\"123\",\"amount\":20,\"currency\":\"AUD\",\"return_path\":\"verification\":\""+verification+"\"}");
	      out.flush();
	      in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
	      String line;
	      while ((line = in.readLine()) != null) {
	          result += line;
	      }
	      System.out.print(result);
	      return new ReturnData(200,"result");
	}
	//获取用户详情
	@RequestMapping("getUserDetail")
	public Object getUserDetail(Long uId){
		return loginService.getUserDetail(uId);
	}
	//获取用户银行卡
	@RequestMapping("getUserbrank")
	public Object getUserbrank(Long uId){
		return loginService.getUserbrank(uId);
	}
}
